package com.gitee.elf.core.data

import com.google.gson.*
import org.bukkit.Color
import taboolib.library.configuration.ConfigurationSection
import java.lang.reflect.Type

class DataColor(val value: Color) : Data {


    override fun getValue(): Any {
        return value
    }

    override fun clone(): Data {
        return DataColor(value)
    }

    companion object {

        fun ConfigurationSection.getColor(it: String): Color {
            return this.getConfigurationSection(it)!!.toColor()
        }

        fun ConfigurationSection.toColor(): Color {
            return Color.fromRGB(getInt("red"), getInt("green"), getInt("blue"))
        }

    }

    @SerializerType(Color::class)
    class Serializer : JsonSerializer<DataColor>, JsonDeserializer<DataColor> {

        override fun serialize(src: DataColor, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            return JsonPrimitive(src.value.asRGB())
        }

        override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): DataColor {
            return DataColor(Color.fromRGB(json!!.asInt))
        }


    }
}